package com.fz.us.oa.web.controller.com;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.base.bean.jqgrid.JqGridFilter;
import com.fz.us.admin.base.bean.jqgrid.JqGridPager;
import com.fz.us.admin.base.bean.search.order.Order;
import com.fz.us.admin.base.bean.search.result.SearchResultPaging;
import com.fz.us.admin.base.utils.DataUtil;
import com.fz.us.admin.base.utils.StringUtils;
import com.fz.us.admin.company.bean.FlowEnum;
import com.fz.us.admin.company.entity.Company;
import com.fz.us.admin.company.entity.TaskEntity;
import com.fz.us.admin.company.entity.User;
import com.fz.us.admin.company.service.DutyService;
import com.fz.us.admin.company.service.TaskEntityService;
import com.fz.us.oa.core.entity.com.BusinessTalk;
import com.fz.us.oa.core.entity.common.FileManage;
import com.fz.us.oa.core.service.com.BusinessTalkService;
import com.fz.us.oa.core.service.com.CustomerInfoService;
import com.fz.us.oa.core.service.common.FileManagerService;
import com.fz.us.oa.web.controller.BaseController;
import com.fz.us.oa.web.shiro.bind.annotation.CurrentUser;
import com.fz.us.oa.web.spring.bind.annotation.RequestJsonParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dzw on 2017/12/1.
 */
@Controller
@RequestMapping(value = "/com/businessTalk")
public class BusinessTalkController extends BaseController {
    private static final long serialVersionUID = 7267051521000206053L;

    @Autowired
    private DutyService dutyService;
    @Autowired
    private TaskEntityService taskEntityService;
    @Autowired
    private CustomerInfoService customerInfoService;
    @Autowired
    private BusinessTalkService businessTalkService;
    @Autowired
    private FileManagerService fileManagerService;

    /**
     * 列表
     */
    @RequestMapping(value = "list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> list(@CurrentUser User user,Model model,
                                    String viewtype,
                                    JqGridPager pager,
                                    @RequestJsonParam(value = "filters", required = false) JqGridFilter filters) {
        Company company = user.getCompany();
        // 排序
        Order order = null;

        SearchResultPaging searchResult = null;
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("company", company);
        params.put("state", new BaseEnumManager.StateEnum[]{BaseEnumManager.StateEnum.Enable, BaseEnumManager.StateEnum.Delete});

        if("self".equals(viewtype)){
            params.put("creater",user);
        }

        searchResult = businessTalkService.paging(null,null,order,
                "com.fz.us.oa.core.entity.com.BusinessTalk",
                new FlowEnum.ProcessState []{FlowEnum.ProcessState.Running, FlowEnum.ProcessState.Backed, FlowEnum.ProcessState.Finished, FlowEnum.ProcessState.Deny}, params);

        List<BusinessTalk> businessTalkList = searchResult.getList();
        ArrayList<Map<String, Object>> dataRows = new ArrayList<Map<String, Object>>();
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String,Object> rMap;

        for(BusinessTalk businessTalk: businessTalkList){
            rMap = new HashMap<String, Object>();
            rMap.put("id",businessTalk.getId());
            rMap.put("name",businessTalk.getCustomerInfo()==null?"":businessTalk.getCustomerInfo().getName());
            rMap.put("talkTopic",businessTalk.getTalkTopic());
            rMap.put("talkType",businessTalk.getTalkType());
            rMap.put("talkAddress", businessTalk.getTalkAddress());
            rMap.put("talkObject", businessTalk.getTalkObject());
            dataRows.add(rMap);
        }

        data.put("dataRows", dataRows);
        data.put("page", searchResult.getPageNumber());
        data.put("rows", searchResult.getPageSize());
        data.put("records", searchResult.getTotalCount());
        data.put("total", searchResult.getPageCount());
        return data;
    }

    /**
     * 新建，编辑页面
     */
    @RequestMapping(value = "input", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String input(@CurrentUser User user, Model model,String keyId,String draft,String index,String todo,String remind,String record) {
        if (StringUtils.isNotEmpty(keyId)){
            BusinessTalk businessTalk = businessTalkService.get(keyId);

            String talkFileId="";
            if(businessTalk.getTalkFile()!=null && businessTalk.getTalkFile().size()>0){
                for(FileManage file:businessTalk.getTalkFile()){
                    talkFileId+=file.getId()+",";
                }
            }

            model.addAttribute("keyId", keyId);
            model.addAttribute("talkFileId", talkFileId);
            model.addAttribute("businessTalk", businessTalk);
        }
        model.addAttribute("draft", draft);
        model.addAttribute("index", index);
        model.addAttribute("todo", todo);
        model.addAttribute("remind", remind);
        model.addAttribute("record", record);
        return "/com/businessTalk_input";
    }

    /**
     * 只读页面
     */
    @RequestMapping(value = "read", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String read(@CurrentUser User user, Model model,String keyId,String draft,String index,String todo,String remind,String record) {
        if(StringUtils.isNotEmpty(keyId)) {
            BusinessTalk businessTalk = businessTalkService.get(keyId);
            TaskEntity taskEntity = taskEntityService.getInfoByKeyId(keyId);
            int stepNo = taskEntity.getStepNo();
            String isEdit =  isEdit(keyId);

            String talkFileId="";
            if(businessTalk.getTalkFile()!=null && businessTalk.getTalkFile().size()>0){
                for(FileManage file:businessTalk.getTalkFile()){
                    talkFileId+=file.getId()+",";
                }
            }

            model.addAttribute("stepNo", stepNo);
            model.addAttribute("isEdit", isEdit);
            model.addAttribute("businessTalk", businessTalk);
            model.addAttribute("talkFileId", talkFileId);
        }

        model.addAttribute("keyId", keyId);
        model.addAttribute("draft", draft);
        model.addAttribute("index", index);
        model.addAttribute("todo", todo);
        model.addAttribute("remind", remind);
        model.addAttribute("record", record);
        return "/com/businessTalk_read";
    }

    public BusinessTalk setData(User user, String keyId, String customerInfoId, String startDate, String endDate, String talkTopic, String talkType,
                                String talkAddress, String talkObject, String talkContent, String talkFileIds, String joinMember){
        BusinessTalk businessTalk=null;
        if(StringUtils.isEmpty(keyId)){
            businessTalk=new BusinessTalk();
            businessTalk.setCompany(user.getCompany());
        }else{
            businessTalk=businessTalkService.get(keyId);
        }
        businessTalk.setCreater(user);
        businessTalk.setCustomerInfo(customerInfoService.get(customerInfoId));
        businessTalk.setStartDate(DataUtil.StringToDate(startDate));
        businessTalk.setEndDate(DataUtil.StringToDate(endDate));
        businessTalk.setTalkTopic(talkTopic);
        businessTalk.setTalkType(talkType);
        businessTalk.setTalkAddress(talkAddress);
        businessTalk.setTalkObject(talkObject);
        businessTalk.setTalkContent(talkContent);
        businessTalk.setJoinMember(joinMember);

        if(StringUtils.isNotEmpty(talkFileIds)){
            List<FileManage> fileManageList=new ArrayList<FileManage>();
            for(String s:talkFileIds.split(",")){
                fileManageList.add(fileManagerService.get(s.trim()));
            }
            businessTalk.setTalkFile(fileManageList);
        }

        return businessTalk;
    }

    /**
     * 保存操作
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public Object save(@CurrentUser User user, String keyId, String customerInfoId, String startDate, String endDate, String talkTopic, String talkType,
                       String talkAddress, String talkObject, String talkContent, String talkFileIds, String joinMember) {

        Map<String,Object> rMap=new HashMap<String,Object>();
        BusinessTalk businessTalk=setData(user,keyId,customerInfoId,startDate,endDate,talkTopic,talkType,talkAddress,talkObject,talkContent,talkFileIds,joinMember);
        if(StringUtils.isNotEmpty(keyId)){
            businessTalkService.update(businessTalk);
            rMap.put("message", "更新成功");
        } else {
            businessTalkService.save(businessTalk, "com.fz.us.oa.core.entity.com.BusinessTalk");
            rMap.put("message","保存成功");
        }
        rMap.put("state", "1");
        rMap.put("keyId", keyId);
        return ajaxJson(rMap);
    }

    /**
     * 提交
     */
    @RequestMapping(value = "commit", method = RequestMethod.POST)
    public Object commit(@CurrentUser User user, String keyId, String customerInfoId, String startDate, String endDate, String talkTopic, String talkType,
                         String talkAddress, String talkObject, String talkContent, String talkFileIds, String joinMember, String curDutyId, String comment) {

        Map<String,Object> rMap=new HashMap<String,Object>();
        BusinessTalk businessTalk=setData(user,keyId,customerInfoId,startDate,endDate,talkTopic,talkType,talkAddress,talkObject,talkContent,talkFileIds,joinMember);

        if (StringUtils.isNotEmpty(keyId)) {
            businessTalkService.approve(businessTalk, dutyService.get(curDutyId), comment);
        } else {
            businessTalkService.commit(businessTalk, "com.fz.us.oa.core.entity.com.BusinessTalk", dutyService.get(curDutyId));
        }
        rMap.put("message", "提交成功");
        rMap.put("state", "1");
        rMap.put("keyId", keyId);
        return ajaxJson(rMap);
    }

    //审批
    @RequestMapping(value = "approve", method = RequestMethod.POST)
    public Object approve(@CurrentUser User user, String keyId,  String curDutyId, String comment){
        Map<String,Object> rMap=new HashMap<String,Object>();
        BusinessTalk businessTalk=businessTalkService.get(keyId);
        businessTalkService.approve(businessTalk, dutyService.get(curDutyId), comment);

        rMap.put("message", "提交成功");
        rMap.put("state", "1");
        rMap.put("keyId", keyId);
        return ajaxJson(rMap);
    }

    //退回
    @RequestMapping(value = "back", method = RequestMethod.POST)
    public Object back(@CurrentUser User user, String keyId,String curDutyId, String comment) {
        if (StringUtils.isNotEmpty(keyId)) {
            BusinessTalk businessTalk=businessTalkService.get(keyId);
            businessTalkService.back(businessTalk, dutyService.get(curDutyId), comment);
        }
        Map<String,Object> rMap=new HashMap<String,Object>();
        rMap.put("message","退回成功");
        rMap.put("state","1");
        rMap.put("keyId", keyId);
        return ajaxJson(rMap);
    }

    //否决
    @RequestMapping(value = "deny", method = RequestMethod.POST)
    public Object deny(@CurrentUser User user, String keyId,String curDutyId, String comment) {
        if (StringUtils.isNotEmpty(keyId)) {
            BusinessTalk businessTalk=businessTalkService.get(keyId);
            businessTalkService.deny(businessTalk, dutyService.get(curDutyId), comment);
        }
        Map<String,Object> rMap=new HashMap<String,Object>();
        rMap.put("message","否决成功");
        rMap.put("state","1");
        rMap.put("keyId", keyId);
        return ajaxJson(rMap);
    }

}
